﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace com.yoozoo.gta.Extension.UGUI.ScreenOrientationEx
{
    [Serializable]
    public class CanvasAdaptConfig
    {
        public Vector2 referenceResolution;
        public CanvasScaler.ScreenMatchMode screenMatchMode;
        public float matchWidthOrHeight;
        
        //重写Equals方法
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            CanvasAdaptConfig other = (CanvasAdaptConfig)obj;
            // Compare referenceResolution properties
            if (referenceResolution != other.referenceResolution)
            {
                return false;
            }

            // Compare screenMatchMode properties
            if (screenMatchMode != other.screenMatchMode)
            {
                return false;
            }

            // Compare matchWidthOrHeight properties
            if (Math.Abs(matchWidthOrHeight - other.matchWidthOrHeight) > 0.001f)
            {
                return false;
            }

            return true;
        }
        
        //重写GetHashCode方法
        public override int GetHashCode()
        {
            int hash = 17;
            hash = hash * 23 + referenceResolution.GetHashCode();
            hash = hash * 23 + screenMatchMode.GetHashCode();
            hash = hash * 23 + matchWidthOrHeight.GetHashCode();
            return hash;
        }
        
    }
    [DisallowMultipleComponent]
    public class CanvasScalerAdapt : AdaptBase<CanvasAdaptConfig, CanvasScaler> {
        public override int Order => (int)AdapterExecuteOrder.CanvasScaler; 
        protected override void CopyProperty(CanvasAdaptConfig config) {
            base.CopyProperty(config);
            config.matchWidthOrHeight = mComponent.matchWidthOrHeight;
            config.screenMatchMode = mComponent.screenMatchMode;
            config.referenceResolution = mComponent.referenceResolution;
        }

        protected override void ApplyConfig(CanvasAdaptConfig config) {
            base.ApplyConfig(config);
            mComponent.referenceResolution = config.referenceResolution;
            mComponent.screenMatchMode = config.screenMatchMode;
            mComponent.matchWidthOrHeight = config.matchWidthOrHeight;
        }
    }

}
